
package com.android.matmanual;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ReceiveTest extends BaseActivity {
	/** Called when the activity is first created. */
	Button btnRecord, btnStop, btnExit;
	boolean isRecording = false;	
	static final int frequency = 44100;
	static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
	static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
	int recBufSize,playBufSize;
	AudioRecord audioRecord;
	AudioTrack audioTrack;
	WakeLock mWakeLock;
	RecordPlayThread rt;
	AudioManager audioManager = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		rt = new RecordPlayThread();
		super.onCreate(savedInstanceState);
		PowerManager pm =(PowerManager) getSystemService(POWER_SERVICE); 
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "XYTEST"); 
		mWakeLock.acquire(); 
		setContentView(R.layout.receive_test);
		setTitle(getResources().getString(R.string.receive_mic));
		recBufSize = AudioRecord.getMinBufferSize(frequency,
				channelConfiguration, audioEncoding);

		playBufSize=AudioTrack.getMinBufferSize(frequency,
				channelConfiguration, audioEncoding);
		// -----------------------------------------
		audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
				channelConfiguration, audioEncoding, recBufSize);

		audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
				channelConfiguration, audioEncoding,
				playBufSize, AudioTrack.MODE_STREAM);
		//------------------------------------------

		audioTrack.setStereoVolume(200f, 200f);

		isRecording = true;
		audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);  

		while(audioManager.getMode() != 2)
		{
			Log.d("lifei-MatManual", "##################### audioManager.getMode try to set IN_CALL");
			audioManager.setMode(2);
		}
		Log.d("lifei-MatManual", "##################### audioManager.getMode:"+audioManager.getMode());
		audioManager.setMicrophoneMute(false);          
		audioManager.setSpeakerphoneOn(true);
		rt.start();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		 rt.stopT();
	//	 audioManager.setMode(AudioManager.MODE_RINGTONE);
		 super.onPause();
	}

	@Override
	protected void onStop() {
		Log.d("lifei", "########################### onStop");
		// TODO Auto-generated method stub
	//	audioManager.setMode(AudioManager.MODE_RINGTONE);
		try {
			isRecording = false;
			if(null != audioTrack)
			{Log.d("lifei", "###########################audioTrack onStop");
				audioTrack.stop();
				audioTrack.release();
			}
			if(null != audioRecord)
			{Log.d("lifei", "###########################audioRecord onStop");
				audioRecord.stop();
				audioRecord.release();
			}
			} catch (Exception e) {
				//Toast.makeText(ReceiveTest.this, t.getMessage(), 1000);
			}
        finish();
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(mWakeLock!=null)
		{
			mWakeLock.release(); 
			mWakeLock = null;
		}
		//android.os.Process.killProcess(android.os.Process.myPid());
	}
	 @Override
	 public boolean onKeyUp(int keyCode, KeyEvent event) {	 
		 	return true;
	 	} 
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
		 	

		 	if(keyCode == KeyEvent.KEYCODE_BACK)
		 	{
				finish();
		 	}	 
		 	return true;
	 	}
	class RecordPlayThread extends Thread {
		public void run() {
			try {
				byte[] buffer = new byte[recBufSize];
				audioRecord.startRecording();
				
				audioTrack.play();
				while (isRecording) {
									
					int bufferReadResult = audioRecord.read(buffer, 0,
							recBufSize);

					byte[] tmpBuf = new byte[bufferReadResult];
					System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);
										
					audioTrack.write(tmpBuf, 0, tmpBuf.length);
					Log.v("leiyonghui","isRecording......");
				}
				audioTrack.stop();
				audioRecord.stop();
			} catch (Exception e) {
				//Toast.makeText(ReceiveTest.this, t.getMessage(), 1000);
			}
		}
		public void stopT()
		{
			try {
			isRecording = false;
			if(null != audioTrack)
			{
				audioTrack.stop();
			}
			if(null != audioRecord)
			{
				audioRecord.stop();
			}
			} catch (Exception e) {
				//Toast.makeText(ReceiveTest.this, t.getMessage(), 1000);
			}
		}
	};
}
		 